package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.SystemLogs;
import com.example.mall.dto.SystemLogsDto;
import com.example.mall.mapper.SystemLogsMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SystemLogsQuery;
import com.example.mall.service.ISystemLogsService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 系统日志表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class SystemLogsServiceImpl extends ServiceImpl<SystemLogsMapper, SystemLogs> implements ISystemLogsService {

    @Autowired
    private SystemLogsMapper systemLogsMapper;

    @Override
    public PageDTO<SystemLogsDto> queryPage(SystemLogsQuery systemLogsQuery) {
        if (systemLogsQuery == null) {
            systemLogsQuery = new SystemLogsQuery();
        }
        Page<SystemLogs> page = systemLogsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SystemLogs> cartPage = lambdaQuery()
                .like(systemLogsQuery.getLogId()!= null, SystemLogs::getLogId, systemLogsQuery.getLogId())
                .like(systemLogsQuery.getLogLevel()!= null, SystemLogs::getLogLevel, systemLogsQuery.getLogLevel())
                .like(systemLogsQuery.getLogMessage()!= null, SystemLogs::getLogMessage, systemLogsQuery.getLogMessage())
                .like(systemLogsQuery.getLogSource()!= null, SystemLogs::getLogSource, systemLogsQuery.getLogSource())
                .like(systemLogsQuery.getIpAddress()!= null, SystemLogs::getIpAddress, systemLogsQuery.getIpAddress())
                .like(systemLogsQuery.getUserId()!= null, SystemLogs::getUserId, systemLogsQuery.getUserId())
                .orderByDesc(SystemLogs::getCreateDate)
                .page(page);
        return PageDTO.of(cartPage, SystemLogsDto.class);
    }

    @Override
    public void insert(SystemLogsDto systemLogsDto) {
        SystemLogs systemLogs = new SystemLogs();
        CopyUtils.copyProperties(systemLogsDto, systemLogs);
        systemLogs.setLogId(CodeGenerateUtils.generateUniqueId());
        systemLogs.setCreateDate(new Date());
        systemLogsMapper.insert(systemLogs);
    }

    @Override
    public void update(SystemLogsDto systemLogsDto) {
        AssertUtil.isNotNull(systemLogsDto.getLogId(), "logId不能为空");
        SystemLogs systemLogs = systemLogsMapper.selectById(systemLogsDto.getLogId());
        AssertUtil.isNotNull(systemLogs, "系统日志表信息不存在");
        CopyUtils.copyProperties(systemLogsDto, systemLogs);
        systemLogsMapper.updateById(systemLogs);
    }

    @Override
    public void delete(Long logId) {
        AssertUtil.isNotNull(logId, "logId不能为空");
        SystemLogs systemLogs = systemLogsMapper.selectById(logId);
        AssertUtil.isNotNull(systemLogs, "系统日志表信息不存在");
        systemLogsMapper.deleteById(logId);
    }

    @Override
    public void deleteBatch(List<Long> logIds) {
        AssertUtil.isNotNull(logIds, "logIds不能为空");
        List<SystemLogs> systemLogs = systemLogsMapper.selectBatchIds(logIds);
        AssertUtil.isNotNull(systemLogs, "系统日志表信息不存在");
        systemLogsMapper.deleteBatchIds(logIds);
    }

    @Override
    public SystemLogs querySystemLogsById(Long logId) {
        AssertUtil.isNotNull(logId, "logId不能为空");
        return systemLogsMapper.selectById(logId);
    }

    @Override
    public List<SystemLogs> querySystemLogsList() {
        return this.list();
    }
}
